package cn.iocoder.yudao.module.ems.controller.admin.maintaintask;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.ems.controller.admin.maintaintask.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintaintask.MaintainTaskDO;
import cn.iocoder.yudao.module.ems.convert.maintaintask.MaintainTaskConvert;
import cn.iocoder.yudao.module.ems.service.maintaintask.MaintainTaskService;

@Api(tags = "管理后台 - 保养任务")
@RestController
@RequestMapping("/ems/maintain-task")
@Validated
public class MaintainTaskController {

    @Resource
    private MaintainTaskService maintainTaskService;

    @PostMapping("/create")
    @ApiOperation("创建保养任务")
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:create')")
    public CommonResult<Long> createMaintainTask(@Valid @RequestBody MaintainTaskCreateReqVO createReqVO) {
        return success(maintainTaskService.createMaintainTask(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新保养任务")
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:update')")
    public CommonResult<Boolean> updateMaintainTask(@Valid @RequestBody MaintainTaskUpdateReqVO updateReqVO) {
        maintainTaskService.updateMaintainTask(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除保养任务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:delete')")
    public CommonResult<Boolean> deleteMaintainTask(@RequestParam("id") Long id) {
        maintainTaskService.deleteMaintainTask(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得保养任务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:query')")
    public CommonResult<MaintainTaskRespVO> getMaintainTask(@RequestParam("id") Long id) {
        MaintainTaskDO maintainTask = maintainTaskService.getMaintainTask(id);
        return success(MaintainTaskConvert.INSTANCE.convert(maintainTask));
    }

    @GetMapping("/list")
    @ApiOperation("获得保养任务列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:query')")
    public CommonResult<List<MaintainTaskRespVO>> getMaintainTaskList(@RequestParam("ids") Collection<Long> ids) {
        List<MaintainTaskDO> list = maintainTaskService.getMaintainTaskList(ids);
        return success(MaintainTaskConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得保养任务分页")
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:query')")
    public CommonResult<PageResult<MaintainTaskRespVO>> getMaintainTaskPage(@Valid MaintainTaskPageReqVO pageVO) {
        PageResult<MaintainTaskDO> pageResult = maintainTaskService.getMaintainTaskPage(pageVO);
        return success(MaintainTaskConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出保养任务 Excel")
    @PreAuthorize("@ss.hasPermission('ems:maintain-task:export')")
    @OperateLog(type = EXPORT)
    public void exportMaintainTaskExcel(@Valid MaintainTaskExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MaintainTaskDO> list = maintainTaskService.getMaintainTaskList(exportReqVO);
        // 导出 Excel
        List<MaintainTaskExcelVO> datas = MaintainTaskConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "保养任务.xls", "数据", MaintainTaskExcelVO.class, datas);
    }

}
